package com.x.erp.dao;

import com.x.erp.model.ImageType;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片类型DAO
 */
@Repository
public class ImageTypeDao extends BaseDao {

	private final static String selectImageTypeListSql = "SELECT bit.* FROM BASE_IMAGE_TYPE bit WHERE 1=1 ";
	/**
	 * 查询图片类型列表
	 * @param imageType
	 * @return
	 * @throws Exception
	 */
	public List<ImageType> selectImageTypeList(ImageType imageType) throws Exception{
		StringBuilder sql = new StringBuilder(selectImageTypeListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, imageType);
		return this.queryForList(sql.toString(), ImageType.class, params, imageType);
	}

	private final static String selectImageTypeNumSql = "SELECT COUNT(bit.IMAGE_TYPE_KEY) FROM BASE_IMAGE_TYPE bit WHERE 1=1 ";
	/**
	 * 查询图片类型数量
	 * @param imageType
	 * @return
	 * @throws Exception
	 */
	public int selectImageTypeNum(ImageType imageType) throws Exception{
		StringBuilder sql = new StringBuilder(selectImageTypeNumSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, imageType);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param imageType
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, ImageType imageType) {
		if(imageType != null) {
			if(StringUtils.isNotEmpty(imageType.getImageTypeName())){
				sql.append("AND bit.IMAGE_TYPE_NAME = ? ");
				params.add(imageType.getImageTypeName());
			}
			if(StringUtils.isNotEmpty(imageType.getImageTypeKey())){
				sql.append("AND biv.IMAGE_TYPE_KEY = ? ");
				params.add(imageType.getImageTypeKey());
			}
		}
	}
}
